import {
    AsyncStorage
} from 'react-native';
import GitHubTrending from 'GitHubTrending'

export const FLAG_STORAGE = {flag_popular: 'popular', flag_trending: 'trending'}

export default class DataRepository {
    constructor(flag) {
        this.flag = flag
        if (flag === FLAG_STORAGE.flag_trending) {
            this.trending = new GitHubTrending()

        }
    }

    fetch(url) {
        return new Promise((resolve, reject) => {
            this.fetchLocalRepository(url)
                .then(result => {
                    if (result) {
                        resolve(result)
                    } else {
                        this.fetchNetRepository(url)
                            .then(result => {
                                resolve(result)
                            })
                            .catch(error => {
                                reject(error)
                            })
                    }
                })
                .catch(e => {
                    this.fetchNetRepository(url)
                        .then(result => {
                            resolve(result)
                        })
                        .catch(error => {
                            reject(error)
                        })
                })
        })
    }

    fetchNetRepository(url) {
        return new Promise((resolve, reject) => {
            if (this.flag === FLAG_STORAGE.flag_trending) {
                this.trending.fetchTrending(url)
                    .then(result => {
                        if (!result) {
                            reject(new Error('response nothing'))
                        } else {
                            this.saveRepository(url, result)
                            resolve(result)
                        }
                    })
            } else {
                fetch(url)
                    .then(response => response.json())
                    .then(result => {
                        if (!result) {
                            reject(new Error('response nothing'))
                            return
                        }
                        resolve(result)
                        this.saveRepository(url, result)
                    })
                    .catch(error => {
                        reject(error)
                    })
            }
        })
    }

    fetchLocalRepository(url) {
        return new Promise((resolve, reject) => {
            AsyncStorage.getItem(url, (error, result) => {
                if (error) {
                    reject(error)
                } else {
                    try {
                        resolve(JSON.parse(result))
                    } catch (e) {
                        reject(e)
                    }
                }
            })
        })
    }

    saveRepository(url, data, callback) {
        if (!url || !data) return;
        let wrapData = Object.assign({update_time: new Date().getTime()}, data)
        AsyncStorage.setItem(url, JSON.stringify(wrapData), callback)
    }

    checkData(longTime) {
        let cDate = new Date()
        let tDate = new Date()
        tDate.setTime(longTime)
        if (cDate.getMonth() !== tDate.getMonth()) return false;
        if (cDate.getDay() !== tDate.getDay()) return false;
        if (cDate.getHours() - tDate.getHours() > 4) return false;
        return true;
    }

    post(url, data) {
        return new Promise((resolve, reject) => {
            fetch(url, {
                method: 'post',
                header: {
                    'Accept'      : 'application/json',
                    'Content-Type': 'application/json',
                },
                body  : JSON.stringify(data)
            })
                .then(response => response.json())
                .then(result => {
                    resolve(result)
                })
                .catch(error => {
                    reject(error)
                })
        })
    }
}